/*
 *
 *  This file is part of Cfu
 *
 *  Cfu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Cfu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Cfu. If not, see <http://www.gnu.org/licenses/>.
 *
 *  configuration.cc - william le ferrand (william@corefarm.com) 29/04/2010 15:30
 *
 */


// STL
#include <stdio.h>
#include <iostream>
#include <sstream>

// QT
#include <QDir>
#include <QFile>
#include <QUrl>
#include <QDomDocument>
#include <QDomElement>
#include <QDebug>
#include <QMetaProperty>
#include <QString>
#include <QApplication>
#include <QThread>

// CFU
#include <configuration.hpp>

configuration_t::configuration_t (QString &confFile) :
  _login(""), 
  _password(""),
  _server("gateway.corefarm.com"),
  _confFile (confFile)
{
  _firstStart = !_tryLoadConfigurationFile();
  std::cout << "@@@ FirstStart: " << _firstStart << std::endl; 
  saveConfigurationToFile(); 
}

bool configuration_t::_tryLoadConfigurationFile() 
{  
  QDir homeDir = QDir::home();
  if(homeDir.exists(_confFile))
    {
      QFile file(homeDir.absoluteFilePath(_confFile));
      if(file.open(QIODevice::ReadOnly))
	{
	  QDomDocument document;
	  QString errorMsg;
	  int errorLine, errorColumn;
	  if(document.setContent(&file, &errorMsg, &errorLine, &errorColumn))
	    {
	      QDomNode root = document.firstChild();
	      for(int i=0 ; i<root.childNodes().count() ; i++)
		{
		  const QDomNode &node = root.childNodes().item(i);
		  if(_hasProperty(node.nodeName()))
		    {
		      setProperty(node.nodeName().toAscii(), node.firstChild().nodeValue());
		    }
		  else
		    {
		      qWarning() << "@@@ Found unknown property" << node.nodeName() << "in configuration file";
		    }
		}
	      return true;
	    }
	  else
	    {
	      qWarning() << "@@@ Error when parsing configuration file at line" <<
		errorLine << "column" << errorColumn << ":" << errorMsg;
	    }
	}
      else
	{
	  qWarning() << "@@@ Could not open configuration file for reading :" << file.errorString();
	}
    }

  return false;
}


void configuration_t::saveConfigurationToFile()
{
  QFile file(QDir::home().absoluteFilePath(_confFile));
  if(file.open(QIODevice::WriteOnly))
    {
      QDomDocument document;

      QDomElement root = document.createElement("naclys");
      document.appendChild(root);

      for(int i=0 ; i<metaObject()->propertyCount() ; i++)
	{
	  QMetaProperty prop = metaObject()->property(i);
	  if(prop.isUser(this))
	    {
	      QVariant propValue = prop.read(this);

	      QDomElement node = document.createElement(prop.name());
	      node.appendChild(document.createTextNode(propValue.toString()));
	      root.appendChild(node);
	    }
	}

      if(file.write(document.toString(2).toUtf8()) < 0)
	{
	  qWarning() << "@@@ Error when writing data to configuration file :" << file.errorString();
	}

      file.close();
    }
  else
    {
      qWarning() << "@@@ Could not open configuration file for writing :" << file.errorString();
    }
}


bool configuration_t::_hasProperty(const QString &propertyName)
{
  for(int i=0 ; i<metaObject()->propertyCount() ; i++)
    {
      if(metaObject()->property(i).name() == propertyName)
	{
	  return true;
	}
    }
  return false;
}


void configuration_t::setLogin(const QString &login)
{
  if(login != _login)
    {
      _login = login;
      emit loginChanged(login);
    }
}

void configuration_t::setPassword(const QString &password)
{
  if(password != _password)
    {
      _password = password;
      emit passwordChanged(password);
    }
}


